<?php
/**
 * 文章管理
 * Date: 2018/5/21
 * Time: 11:53
 */

namespace backend\controllers;


use backend\component\BackendBaseController;
use backend\component\ResultFormat;
use backend\service\ArticleService;
use common\models\info\Article;
use backend\service\UploadService;
use yii\helpers\ArrayHelper;

class ArticleController extends BackendBaseController
{
    public function actions()
    {
        return ArrayHelper::merge(parent::actions(),[
            "modify" => [
                'class' => "backend\actions\ModifyAction",
                'modelName'=> Article::className(),
            ],
            "delete" =>[
                'class' => "backend\actions\DeleteAction",
                'modelName'=> Article::className(),
                'is_physics' => true
            ],
            'editor' => [
                'class' => 'common\actions\UEditorActionI',
                'config' => [
                    'imageUrlPrefix' => \Yii::$app->params["attach.image.url"],
                    'imagePathFormat' =>  \Yii::$app->params["attach.image.format"],
                ]
            ],
        ]);
    }
    public function actionVerify(){
        $model = new Article();
        return ResultFormat::success('SUCCESS',$model::$statusArrayList);
    }
    public function actionVerifyload(){
        $post = \Yii::$app->request->post("form");
        $id =$post['id'];
        return ArticleService::auditArticle($id,$post);
    }
    public function actionList() {
            $get = \Yii::$app->request->get();
            $data = ArticleService::articleList($get);
            return ResultFormat::success('SUCCESS',json_decode($data));
    }
    public function actionStatus(){
        $model = new Article();
       $list = $model::$statusDropdownList;
        return ResultFormat::success('SUCCESS',$list);
    }
    public function actionUpload()
    {
          $upload = new UploadService();
          return  $upload->actionMain( $_POST['file']['text']);
    }

    /**
     * 编辑
     * @return mixed
     */
    public function actionEdit() {
        $id = \Yii::$app->request->get("id",0);
        $model = Article::modelById($id);
             $tags = $model->getArticleTags();
            $result['data'] = $model->attributes;
            $result['tags'] = $tags;
        return ResultFormat::success('SUCCESS',$result);
    }

    /*
     * 审核文章提交
     */
    public function actionAudit($id) {
        $post = \Yii::$app->request->post("form");
        return ArticleService::auditArticle($id,$post);
    }

    public function actionNew() {
        $content = \Yii::$app->request->post("content");
        return \GuzzleHttp\json_encode(['content'=>ArticleService::addTagUrl($content)]);
    }
}
